import { Col, Descriptions, Divider, Row, Typography, message } from 'antd';
import styles from './index.less';
import { UNITS, CardDetailLABELS } from './components/dictionary';
import type { CardDataType } from './components/type';

const { Text } = Typography;
/**
 * 权限控制
 *
 * @param {*} buttonType
 * @param {*} buttonPermissions
 * @returns {boolean}
 *
 * 用法：if (!havePermissions('pendingRemoveButtonV2', buttonPermissions)) return;
 */
export const havePermissions = (buttonType: string, buttonPermissions: any[]) => {
  const hasSave = buttonPermissions?.some((item) => item.menuCode === buttonType);
  if (!hasSave) {
    message.info('请联系管理员获取相关权限');
    return false;
  }
  return true;
};

/**
 * 约束信息/市场总体情况
 *
 * @param {{ label: string; value?: React.ReactNode; unit?: string }} param0
 * @param {string} param0.label
 * @param {React.ReactNode} param0.value
 * @param {string} param0.unit
 * @param {string} param0.className
 * @returns {*}
 */
export const InfoItem = ({
  label,
  value,
  unit,
  className,
}: {
  label: string;
  value?: React.ReactNode;
  unit?: string;
  className?: string;
}) => (
  <span className={styles.textInfo}>
    {label}：
    <Text className={className ? className : styles.number}>
      {value == null || (typeof value === 'string' && value.trim() === '') ? '-' : value} {unit}
    </Text>
  </span>
);

/**
 * 企业成交情况
 *
 * @param {{ label: string; value?: React.ReactNode; unit?: string }} param0
 * @param {string} param0.label
 * @param {React.ReactNode} param0.value
 * @param {string} param0.unit
 * @returns {*}
 */
export const CardInfoItem = ({
  label,
  value,
  unit,
}: {
  label: string;
  value?: React.ReactNode;
  unit?: string;
}) => (
  <Col span={12} className={styles.cardInfoItem}>
    <span className={styles.cardInfoItemName}>{label}：</span>
    <span className={styles.cardInfoItemNum}>
      {value == null || (typeof value === 'string' && value.trim() === '') ? '-' : value} {unit}
    </span>
  </Col>
);

/**
 * 方案
 *
 * @param {{ card: CardDataType, proxy?: boolean }} param0
 * @param {CardDataType} param0.card
 * @returns {*}
 */
export const CardDetail = ({ card, proxy }: { card: CardDataType; proxy?: boolean }) => (
  <>
    <Row gutter={45}>
      <CardInfoItem
        label={CardDetailLABELS.TRADE_LIMIT}
        value={card.tradeLimit}
        unit={UNITS.DELISTED}
      />
      <CardInfoItem
        label={CardDetailLABELS.TRADED_ELECTRICITY}
        value={card.tradedElectricity}
        unit={UNITS.DELISTED}
      />
    </Row>
    <Row gutter={45}>
      <CardInfoItem
        label={CardDetailLABELS.LISTING_ELECTRICITY}
        value={card.listingElectricity}
        unit={UNITS.DELISTED}
      />
      <CardInfoItem
        label={CardDetailLABELS.SURPLUS_TRADE_LIMIT}
        value={card.surplusTradeLimit}
        unit={UNITS.DELISTED}
      />
    </Row>
    <Row gutter={45}>
      <CardInfoItem label={CardDetailLABELS.DEAL_NUM} value={card.dealNum} unit={UNITS.COUNT} />
      {!proxy && (
        <CardInfoItem
          label={CardDetailLABELS.DEAL_AVG_PRICE}
          value={card.dealAvgPrice}
          unit={UNITS.POWER}
        />
      )}
    </Row>
    <Divider style={{ margin: '16px 0' }} />
    <Descriptions column={1}>
      <Descriptions.Item label={CardDetailLABELS.STATION_NAME}>
        {card.stationName ?? '-'}
      </Descriptions.Item>
      <Descriptions.Item label={CardDetailLABELS.MARK_DATE}>
        {card.markDate ?? '-'}
      </Descriptions.Item>
      <Descriptions.Item label={CardDetailLABELS.REPORT_TIME}>
        {card.reportTime ?? '-'}
      </Descriptions.Item>
      <Descriptions.Item label={CardDetailLABELS.PRICE_RANGE}>
        {card.priceRange ?? '-'}
      </Descriptions.Item>
      <Descriptions.Item label={CardDetailLABELS.QUANTITY_RANGE}>
        {card.quantityRange ?? '-'}
      </Descriptions.Item>
      <Descriptions.Item label={CardDetailLABELS.TOTAL_DELISTED}>
        {card.totalDelisted ?? '-'}
      </Descriptions.Item>
    </Descriptions>
  </>
);

/**
 * 定义空格校验函数
 *
 * @param {string} fieldName
 * @return {*}
 */
export const validateNoSpaces = (fieldName: string) => {
  return (_: any, value: any) => {
    if (value && value.trim() === '') {
      return Promise.reject(`${fieldName}不能为空格!`);
    }
    return Promise.resolve();
  };
};
